﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Reflection;

namespace DynamicProxy
{
    public static class LogHelper
    {
        /// <summary>
        /// 添加日志文件
        /// </summary>
        /// <param name="text"></param>
        public static void AddLog(string text)
        {
            AddLog(text, "DynamicProxy.log");
        }

        /// <summary>
        /// string扩展方法：添加日志文件
        /// </summary>
        /// <param name="text">要添加的内容</param>
        /// <param name="fileName">文件的相对路径需带后缀名</param>
        /// <returns>1:</returns>
        public static bool AddLogFiles(this string text, string fileName)
        {
            return AddLog(text, fileName);
        }
        /// <summary>
        /// 添加日志文件
        /// </summary>
        /// <param name="text"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool AddLog(string text, string fileName)
        {
            try
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath("/" + fileName);
                string dir = filePath.Substring(0, filePath.LastIndexOf("\\"));
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
                using (System.IO.StreamWriter srFile = new System.IO.StreamWriter(filePath, true))
                {
                    srFile.WriteLine(text);
                    return true;
                }
            }
            catch (Exception)
            {
                throw;

            }
        }


    }
}
